﻿using System.Linq;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Entities;
namespace SportsStore.Domain.Concrete
{
    public class EFProductRepository : IProductRepository
    {
        private EFDbContext context = new EFDbContext();

        public IQueryable<Product> Products
        {
            get { return context.Products.Include("Category"); }
        }

        public void SaveProduct(Product product)
        {
            try
            {


                if (product.ProductID == 0)
                {
                    context.Products.Add(product);
                }
                else
                {
                    //Product p = context.Products.FirstOrDefault<Product>(c => c.ProductID == product.ProductID);
                    //p.Price = product.Price;
                    //p.Name = product.Name;
                    //p.ImageMimeType = product.ImageMimeType;
                    //p.ImageData = product.ImageData;
                    //p.Description = product.Description;
                    //p.Category = product.Category;

                    context.Entry(product).State = System.Data.EntityState.Modified;
             //       context.Entry(product).Reference("Category").EntityEntry.State = System.Data.EntityState.Modified;

                }
                context.SaveChanges();
            }
            catch (System.Exception ex)
            {

                throw ex;
            }
        }

        public void DeleteProduct(Product product)
        {
            context.Products.Remove(product);
            context.SaveChanges();
        }
    }
}